//=================================================================
// 
//  window.cpp
// 
// Written by Luke Olsen, Jan. 2004
// Based on example code distributed with Qt under GPL license.
//
// Provides a bare-bones implementation of a GL widget under Qt.
// Code stubs are provided; have to add code to draw to the 
// screen using OpenGL API calls.
// 
//=================================================================

#include <QtGui>
#include "window.h"
#include "main.h"


Window::Window( QWidget* parent )
    : QWidget( parent )
{
    // Set window title
    setWindowTitle(tr("Tree EduSimulator"));

    // Create top-level layout manager
   // QVBoxLayout* mainLayout = new QVBoxLayout();
    QHBoxLayout* mainLayout = new QHBoxLayout();
    QVBoxLayout* sideBarLayout = new QVBoxLayout();


    // Create an OpenGL widget
    glwidget = new Renderer(this);
    glwidget -> setSizePolicy( QSizePolicy :: Expanding,  QSizePolicy :: Expanding);

    // Create a menu
    QMenu *file = new QMenu(tr("&File"), this);
    file->addAction( "Load Points", glwidget, SLOT(loadPoints()));//loading the attraction points
    file->addAction( "Save Points", glwidget, SLOT(savePoints()));//saving the attraction points
    file->addAction( "Generate Points", glwidget, SLOT(createPoints()));//Generating the attractions point
    file->addSeparator();
    file->addAction("Load Texture", glwidget, SLOT(loadTexture()));
    file->addAction( "Reset Rotation", glwidget, SLOT(resetTrackball()), Qt::CTRL+Qt::Key_R);
    file->addSeparator();
    file->addAction( "Exit",  qApp, SLOT(quit()), Qt::CTRL+Qt::Key_Q );

    QMenu *view = new QMenu(tr("&View"),this);
    view->addAction( "Skeleton Draw", glwidget, SLOT(selectSkeleton()), Qt::CTRL+Qt::Key_A );
    view->addAction( "WireFrame", glwidget, SLOT(selectWireFrame()), Qt::CTRL+Qt::Key_D );
    view->addAction( "Smooth Shading", glwidget, SLOT(selectSmoothShading()), Qt::CTRL+Qt::Key_S );

    // Create a menu bar
    QMenuBar *m = new QMenuBar();
    m->addMenu( file );
    m->addMenu( view );
    mainLayout->setMenuBar( m );


    mainLayout->addWidget(glwidget);
    mainLayout -> addLayout(sideBarLayout);

    QLabel *treeSizeLabel = new QLabel("Change tree's total size");
    sideBarLayout -> addWidget(treeSizeLabel);
    QSlider *sliderSize = new QSlider(Qt :: Horizontal ,this);
    sliderSize->setRange(1,6);
    sliderSize->setValue(3);
    sideBarLayout->addWidget(sliderSize);

    QLabel *radLabel = new QLabel("Radius of Influence: 0.4");
    glwidget->setLabelRadius(radLabel);
    sideBarLayout -> addWidget(radLabel);
    
    QSlider *sliderInfluence = new QSlider(Qt :: Horizontal ,this);
    sliderInfluence->setRange(3,10);
    sliderInfluence->setValue(4);
    sideBarLayout->addWidget(sliderInfluence);

    QLabel *tropLabel = new QLabel("Tropism coefficent: 0");
    glwidget->setLabelTropism(tropLabel);
    sideBarLayout -> addWidget(tropLabel);

    QSlider *slider = new QSlider(Qt :: Horizontal ,this);//keep this 4 later
    slider -> setRange(0,10);
    slider -> setValue(0);
    sideBarLayout->addWidget(slider);
    
    QLabel *killLabel = new QLabel("Kill Distance: 0.1");
    glwidget->setLabelKill(killLabel);
    sideBarLayout -> addWidget(killLabel);
    
    QSlider *killSlider = new QSlider(Qt:: Horizontal, this);
    killSlider -> setRange(1,10);
    killSlider -> setValue(2);
    sideBarLayout->addWidget(killSlider);

    QPushButton *growButton = new QPushButton("Grow", this);
    growButton->setShortcut(Qt::Key_G);
    sideBarLayout->addWidget(growButton);

    QRadioButton *treeButton = new QRadioButton("Regular Tree", this);
    treeButton -> setChecked(true);
    sideBarLayout->addWidget(treeButton);
    QRadioButton *pineButton = new QRadioButton("Pine Tree", this);
    sideBarLayout->addWidget(pineButton);

    QSpacerItem *spacer = new QSpacerItem(1,1, QSizePolicy :: Expanding,  QSizePolicy :: Expanding);
    sideBarLayout -> addSpacerItem(spacer);

    QCheckBox *textureCBox = new QCheckBox("Disappear Texture");
    sideBarLayout ->addWidget(textureCBox);

    connect(slider, SIGNAL(valueChanged(int)),glwidget, SLOT(tropismTreeModel(int)));
    connect(sliderSize, SIGNAL(valueChanged(int)),glwidget, SLOT(changeTreeSize(int)));
    connect(sliderInfluence, SIGNAL(valueChanged(int)),glwidget, SLOT(changeInfluence(int)));
    connect(killSlider, SIGNAL(valueChanged(int)),glwidget, SLOT(changeKill(int)));
    connect(growButton,SIGNAL(clicked()), glwidget, SLOT(simulatePlant()));
    connect(treeButton, SIGNAL(pressed()), glwidget, SLOT(changeToRegularMode()));
    connect(pineButton, SIGNAL(pressed()), glwidget, SLOT(changeToPineMode()));
    connect(textureCBox, SIGNAL(toggled(bool)), glwidget, SLOT(changeDisapperTexture(bool)));

    // Start the geometry management
    setLayout(mainLayout);
}

